#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<string.h>

using namespace std;
const int maxint = 10000;
char A[maxint], B[maxint]; //前序序列，中序序列
int length;
typedef char ElemType;
typedef struct BinaryTNode {
    ElemType data;
    struct BinaryTNode *lchild, *rchild;
} BinaryTNode, *BinaryTree;

BinaryTree create_BinaryTree(const ElemType A[], const ElemType B[], int la, int ha, int lb, int hb) {
    //A[]存放前序序列，B[]存放中序序列。la和ha分别表示前序序列的起始位置和最终位置，lb和hb分别表示中序序列的起始位置和最终位置
    int llen, rlen;  //中序序列中左右子树的长度
    int s = 0;
    BinaryTree T = (BinaryTree) malloc(sizeof(BinaryTNode));
    T->data = A[la];
    T->lchild = T->rchild = NULL;
    while (A[la] != B[s]) //s表示跟结点在中序序列中的位置
        s++;
    llen = s - lb;
    rlen = hb - s;

    if (llen != 0) {
        T->lchild = create_BinaryTree(A, B, la + 1, la + llen, lb, lb + llen - 1);
    } else {
        T->lchild = NULL; //左子树长度为零时，左孩子为空
    }

    if (rlen != 0) {
        T->rchild = create_BinaryTree(A, B, ha - rlen + 1, ha, hb - rlen + 1, hb);
    } else {
        T->rchild = NULL;//右子树长度为零时，有孩子为空
    }
    return T;
}

void postoder(BinaryTree T, vector <ElemType> &res) {
    if (T != NULL) {
        postoder(T->lchild, res);
        postoder(T->rchild, res);
        res.push_back(T->data);
    }
}

int main() {
    cin.getline(A, maxint);
    cin.getline(B, maxint);

    string strA(A);
    string strB(B);
    // 去除空格
    strA.erase(::remove_if(strA.begin(), strA.end(), ::isspace), strA.end());
    strB.erase(::remove_if(strB.begin(), strB.end(), ::isspace), strB.end());

    BinaryTree T = create_BinaryTree(strA.c_str(), strB.c_str(), 0, strA.size()-1, 0, strB.size()-1);

    vector <ElemType> res;
    postoder(T, res);//后序遍历二叉树检验结果
    for (int i = 0; i < res.size(); i++) {
        cout << res[i];
        if (i != res.size() - 1) {
            cout << ",";
        }
    }
    cout << endl;
    system("pause");
    return 0;
}